//
//  LogintView.swift
//  SwiftDemo
//
//  Created by admin on 2020/10/15.
//

import UIKit

class LogintView: UIView {
    /// 哪里跳转
    enum WherePush {
        case register
        case noLogin
    }

    // 背景颜色
    var backView: UIView = UIView()

    // 手机号
    var phoneTextF: UITextField = UITextField()
    // 密码
    var passWordTextF: UITextField = UITextField()
    // 登录按钮
    var loginBtn: UIButton = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViewAction()
    }

    //    required 父类必须实现的
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LogintView {
    func setupViewAction() {
        backView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        backView.backgroundColor = UIColor.white
        addSubview(backView)

        // 间隔
        let sc_w = 20
        phoneTextF.frame = CGRect(x: sc_w, y: 100, width: Int(SCREEN_WIDTH) - sc_w * 2, height: 40)
        phoneTextF.backgroundColor = HEXCOLOR(c: 0x888888)
        phoneTextF.layer.cornerRadius = 5
        addSubview(phoneTextF)

        passWordTextF.frame = CGRect(x: sc_w, y: 180, width: Int(SCREEN_WIDTH) - sc_w * 2, height: 40)
        passWordTextF.backgroundColor = HEXCOLOR(c: 0x888888)
        passWordTextF.layer.cornerRadius = 5
        addSubview(passWordTextF)

        loginBtn.frame = CGRect(x: sc_w, y: 300, width: Int(SCREEN_WIDTH) - sc_w * 2, height: 50)
        loginBtn.backgroundColor = UIColor.red
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.addTarget(self, action: #selector(loginCilck), for: UIControl.Event.touchUpInside)

        addSubview(loginBtn)

        let iconImageViewFrame = CGRect(x: sc_w, y: 400, width: 100, height: 100)
        let iconImageView = UIButton(frame: iconImageViewFrame)
        iconImageView.layer.cornerRadius = 25
//        iconImageView.clipsToBounds = true
//        iconImageView.layer.masksToBounds = true
        addSubview(iconImageView)
        iconImageView.setImage(R.image.icon_default_voice_animation(), for: .normal)
//        iconImageView.imageView?.image = UIImage(named: "icon_default_voice_animation")
        iconImageView.contentMode = .scaleAspectFill
        iconImageView.backgroundColor = UIColor.red
//        iconImageView.setCornerImage()

        let buttonFrame = CGRect(x: sc_w + 200, y: 400, width: 100, height: 100)
        let buttonView = UIImageView(frame: buttonFrame)
//        buttonView.contentMode = .scaleAspectFill
//        buttonView.layer.cornerRadius = 25
        buttonView.clipsToBounds = true
//        buttonView.layer.masksToBounds = true
        buttonView.image = UIImage(named: "icon_default_voice_animation")
        buttonView.backgroundColor = UIColor.red
//        buttonView.cornerRadius = 25
//        buttonView.setCornerImage()
//        let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
        buttonView.zy_cornerRadius(cornerRadius: 30)
        addSubview(buttonView)
    }

    @objc func loginCilck(btn: UIButton) {
//        let sheet = UIAlertController(title: "标题", message: nil, preferredStyle: .actionSheet)
//
//        let attStr = NSMutableAttributedString(string: "提示: 未查看實際樓層的樓盤將自動扣除查看限額", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13)])
//        sheet.setValue(attStr, forKey: "attributedMessage")
//
//        let ac2 = UIAlertAction(title: "新增全新表格", style: .destructive, handler: nil)
//        sheet.addAction(ac2)
//        sheet.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
//        UIApplication.wg_rootViewController().present(sheet, animated: true, completion: nil)

        let textObject = QQApiTextObject(text: "23456765432")
        let req = SendMessageToQQReq(content: textObject)
        QQApiInterface.send(req)

        let alerVC = UIAlertController(title: "提示", message: "是否登录", preferredStyle: .alert)
        let alerView = UIAlertAction(title: "确定", style: .destructive, handler: { _ in

            guard let window = UIApplication.shared.delegate?.window else {
                return
            }

            if self.phoneTextF.text == "1" && self.passWordTextF.text == "1" {
                let uDefault = UserDefaults.standard
                uDefault.set("1", forKey: "login")
                uDefault.synchronize()

                let tabbar = TabBarViewController()
                window?.rootViewController = tabbar
                window?.makeKeyAndVisible()

            } else {
            }

        })
        let alerViewCancel = UIAlertAction(title: "取消", style: .default, handler: { _ in
        })
        alerVC.addAction(alerView)
        alerVC.addAction(alerViewCancel)
        UIApplication.wg_rootViewController().present(alerVC, animated: true, completion: nil)
    }
}

extension UIImageView {
    func setCornerImage() {
        // 异步绘制图像
        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0)

        // 获取当前上下文
        let ctx = UIGraphicsGetCurrentContext()

        // 设置填充颜色
        UIColor.red.setFill()
        UIRectFill(bounds)

        let point = CGPoint(x: 30, y: 30)
        let point2 = CGPoint(x: 30, y: 30)

        ctx?.addArc(tangent1End: point, tangent2End: point2, radius: 30)
        // 2.添加圆及裁切
//        ctx?.addEllipse(in: self.bounds)
        // 裁切
//        ctx?.clip()

        // 3.绘制图像
        draw(bounds)

        // 4.获取绘制的图像
        let image = UIGraphicsGetImageFromCurrentImageContext()

        // 5关闭上下文
        UIGraphicsEndImageContext()

        DispatchQueue.main.async(execute: {
            self.image = image
        })
    }

    func zy_cornerRadius(cornerRadius: CGFloat) {
        let size = bounds.size

        let scale = UIScreen.main.scale

        let cornerRadii = CGSize(width: cornerRadius, height: cornerRadius)

//        DispatchQueue.global(qos: .default).async(execute: { [self] in

        UIGraphicsBeginImageContextWithOptions(size, false, scale)

//        DispatchQueue.main.async(execute: { [self] in
            // 设置填充颜色
            UIColor.white.setFill()
            UIRectFill(bounds)
//        })

        if nil == UIGraphicsGetCurrentContext() {
            return
        }

        let cornerPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: cornerRadii)
        cornerPath.addClip()
        let color = UIColor.yellow
        color.set()
        cornerPath.stroke()
        self.image?.draw(in: self.bounds)

        let processedImageRef = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
        UIGraphicsEndImageContext()

        DispatchQueue.main.async(execute: { [self] in
            layer.contents = processedImageRef
            //            self.image = newImage;

        })

//        })
    }
}
